package com.fxust.dialog;

import com.fxust.thread.UpLoadPicThread;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @auto johnking
 * @create 2017-12-17 上午11:38
 **/
public class UploadPicDialog extends BaseDialog{

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = -3793192615583879556L;

    /**
     * 界面控件声明
     */
    private final JPanel contentPanel = new JPanel();
    private JButton upLoadButton = null;
    private JButton browseButton = null;
    private JTextField picPathField = null;

    //全局uuid
    private String uuid;

    /**
     * 创建一个新的实例UploadPicDialog.
     * @param frame
     * @param title
     * @param uuid
     */
    public UploadPicDialog(Frame frame, String title, String uuid) {
        super(frame, title);
        this.uuid = uuid;

        setBounds(100, 100, 460, 300);
        getContentPane().setLayout(new BorderLayout());

        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setLayout(null);
        getContentPane().add(contentPanel, BorderLayout.CENTER);

        {
            JLabel filepathLabel = new JLabel("文件路径：");
            filepathLabel.setBounds(10, 45, 65, 15);
            filepathLabel.setHorizontalAlignment(SwingConstants.RIGHT);
            contentPanel.add(filepathLabel);

            // 文件路径TextField
            picPathField = new JTextField();
            picPathField.setBounds(80, 42, 201, 21);
            picPathField.setColumns(10);
            contentPanel.add(picPathField);

            browseButton = new JButton("浏览");
            browseButton.setBounds(280, 41, 60, 23);
            contentPanel.add(browseButton);

            // 开始查询Button
            upLoadButton = new JButton("开始上传");
            upLoadButton.setBounds(350, 41, 90, 23);
            contentPanel.add(upLoadButton);
        }
        {
            JLabel filepathLabel = new JLabel("上传进度：");
            filepathLabel.setBounds(10, 80, 65, 21);
            filepathLabel.setHorizontalAlignment(SwingConstants.RIGHT);
            contentPanel.add(filepathLabel);
            // 详情进度TextField
            dealNumTextField = new JTextField();
            dealNumTextField.setEditable(false);
            dealNumTextField.setColumns(10);
            dealNumTextField.setBounds(80, 80, 110, 21);
            contentPanel.add(dealNumTextField);

            // 详情进度ProgressBar
            dealProgressBar = new JProgressBar();
            dealProgressBar.setMaximum(100);
            dealProgressBar.setBounds(200, 80, 240, 21);
            contentPanel.add(dealProgressBar);
        }
        addListener();
    }


    private void addListener() {
        // [路径选择]的点击事件
        browseButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                browseFilePathClicked();
            }
        });
        // [开始下载Button]的点击事件
        upLoadButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                startUpLoadPicMain();
            }
        });

    }

    private void startUpLoadPicMain() {
        // 验证界面值得有效性
        String filePath = picPathField.getText();
        if (!varifyFilePath(filePath)) {
            return;
        }
        // 启动线程
        new UpLoadPicThread(uuid,filePath).start();
    }

    /**
     * 浏览文件路径
     * @author fgq 2017年8月8日 下午3:26:25
     */
    private void browseFilePathClicked() {
        String filepath = openFileChooser(JFileChooser.DIRECTORIES_ONLY, null, null);
        if (StringUtils.isNotBlank(filepath)) {
            picPathField.setText(filepath);
            if (upLoadButton.isEnabled() == false) {
                upLoadButton.setEnabled(true);
            }
            picPathField.setEditable(false);
        }
    }

}
